goog.provide('czk.ui.Select');

goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.ui.Select');
goog.require('goog.ui.FlatMenuButtonRenderer');

goog.require('czk.ui.ZKComponent');

/**
 * @constructor
 * @extends {czk.ui.ZKComponent}
 */
czk.ui.Select =
    function() {
      goog.base(this);
      this.blockZKChildrenAdd = true;
      this.hasZKChildren = false;
      this.select =
          new goog.ui.Select(null, null, goog.ui.FlatMenuButtonRenderer
              .getInstance());
    };
goog.inherits(czk.ui.Select, czk.ui.ZKComponent);

czk.ui.Select.prototype.select = null;

czk.ui.Select.prototype.initialize =
    function(name, parameter, portlet, children) {
      goog.base(this, 'initialize', name, parameter, portlet, children);
      goog.array.forEach(children, function(c, pos) {
        var label = c[2]['label'];
        this.select.addItem(new goog.ui.MenuItem(label, c[1]));
        if (label == this.value)
          this.select.setSelectedIndex(pos);
      }, this);
    };

czk.ui.Select.prototype.createDom = function() {
  var dh = this.getDomHelper();
  var el = dh.createDom(goog.dom.TagName.DIV, {
    'className': this.className
  });
  this.setElementInternal(el);

  this.addChild(this.select, true);
};

/** @inheritDoc */
czk.ui.Select.prototype.enterDocument =
    function() {
      goog.base(this, 'enterDocument');
      this.getHandler().listen(this.select, goog.ui.Component.EventType.ACTION,
          this.change, false, this);

    };

czk.ui.Select.prototype.change = function() {
  var data = '{"value":"' + this.select.getSelectedItem().getContent() + '"}';
  this.portlet.sendXhr(this, 'onChange', data);

};

czk.ui.Select.prototype.handleZKResponseCommand = function(command) {
  goog.base(this, 'handleZKResponseCommand', command);
  this.getElement()['value'] = this.value;
};

czk.ui.Select.create = function() {
  return new czk.ui.Select();
};
